package com.ifp.weixin.MessageMng.Dao.resp.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;

import com.ifp.weixin.MessageMng.Dao.resp.RespReqdealRelaDao;
import com.ifp.weixin.MessageMng.Message.req.ReqLocationMessage;
import com.ifp.weixin.MessageMng.Message.resp.RespReqdealRela;
import com.sun.org.apache.bcel.internal.generic.NEW;

@Service("respReqdealRelaDao")
public class RespReqdealRelaDaoImpl implements RespReqdealRelaDao {
	
	private HibernateTemplate hibernateTemplate;
	
	public List<RespReqdealRela> getRelas() {
		// TODO 自动生成方法存根
		return (List<RespReqdealRela>)this.hibernateTemplate.find("from RespReqdealRela").listIterator();
	}

	public RespReqdealRela loadById(long id) {
		// TODO 自动生成方法存根
		return this.hibernateTemplate.load(RespReqdealRela.class, id);
	}

	public void save(RespReqdealRela u) {
		// TODO 自动生成方法存根
		hibernateTemplate.save(u);
	}

	/**
	 * @return hibernateTemplate
	 */
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	/**
	 * @param hibernateTemplate 要设置的 hibernateTemplate
	 */
	
	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public List<RespReqdealRela> getRelasByEventKey(String key) {
		// TODO 自动生成方法存根
		String qString = " from RespReqdealRela t where t.key=? ";
		
		// (List<RespReqdealRela>)this.hibernateTemplate.findByNamedQuery( qString,key );
		 return (List<RespReqdealRela>)this.hibernateTemplate.find( qString, key );
		// return this.hibernateTemplate.findByCriteria(criteria)(qString, key);
		 
		 
		 
	}

	public List<RespReqdealRela> getRelasByFeature(String feature) {
		// TODO 自动生成方法存根
		String qString = " from RespReqdealRela t where t.Feature=? ";
		
		return (List<RespReqdealRela>)this.hibernateTemplate.find( qString,feature );
		
		
	}

	public List<RespReqdealRela> getRelasByEventAndKey(String event, String key) {
		// TODO 自动生成方法存根
		String qString = " from RespReqdealRela t where t.Event=?  and  key=? ";
		
		return (List<RespReqdealRela>)this.hibernateTemplate.find( qString,new Object[]{event,key} );
	}

	public List<RespReqdealRela> getRelasByEventAndKey1(String event, String eventKey) {
		// TODO 自动生成方法存根
		final DetachedCriteria detachedCriteria = DetachedCriteria.forClass(RespReqdealRela.class); 
		
		
		if (!("".equals(event)) &&  !(event ==null ) ) {
			detachedCriteria.add(Restrictions.eq("Event",event));
		}
		
		if ( !("".equals(eventKey))   && !( eventKey ==null ) ) {
			detachedCriteria.add(Restrictions.eq("key",eventKey));
		}		


		return ( List<RespReqdealRela>) hibernateTemplate.execute( new HibernateCallback() {
				public Object doInHibernate(Session session) throws HibernateException {
					Criteria criteria = detachedCriteria.getExecutableCriteria(session);
					return criteria.list();
				}
			}); 

	}

}
